package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.pojo.MarketAd;
import com.cskaoyan.bean.pojo.MarketAddress;
import com.cskaoyan.bean.pojo.MarketRole;
import com.cskaoyan.bean.pojo.MarketUser;
import com.cskaoyan.bean.vo.WxAddressListVO;
import com.cskaoyan.service.WxAddressService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Tang_XueCheng
 * @description Wx地址模块
 * @since 2022/04/08 20:05
 */
@RestController
@RequestMapping("wx")
public class WxAddressController {

    @Autowired
    WxAddressService wxAddressService;

    @RequestMapping("address/list")
    public BaseRespVo list() {
        Subject subject = SecurityUtils.getSubject();
        MarketUser principal = (MarketUser) subject.getPrincipal();
        Integer userId = principal.getId();
        WxAddressListVO wxAddressListVO = wxAddressService.queryAddressList(userId);
        return BaseRespVo.ok(wxAddressListVO);
    }

    @RequestMapping("address/detail")
    public BaseRespVo detail(Integer id) {
        MarketAddress marketAddress = wxAddressService.queryAddressDetail(id);
        return  BaseRespVo.ok(marketAddress);
    }

    @RequestMapping("address/save")
    public BaseRespVo save(@RequestBody @Validated MarketAddress marketAddress) {
        //前端传回的id=0，说明是insert语句，要新建一个地址，此时需要返回id
        if (marketAddress.getId() == 0) {
            Subject subject = SecurityUtils.getSubject();
            MarketUser principal = (MarketUser) subject.getPrincipal();
            Integer userId = principal.getId();
            marketAddress.setUserId(userId);
            Integer addressID = wxAddressService.insertAddress(marketAddress);
            return BaseRespVo.ok(addressID);
        } else {
        //前端传回的id！=0，说明是update语句，返回0就可以了

            wxAddressService.updateAddress(marketAddress);
            return BaseRespVo.ok(0);
        }

    }

    @RequestMapping("address/delete")
    public BaseRespVo delete(@RequestBody MarketAddress marketAddress1) {
        marketAddress1.setDeleted(true);
        wxAddressService.deleteAddress(marketAddress1);
        return BaseRespVo.ok();
    }

}
